package TeamShadowCry.Source.Project.Process.Packs;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import TeamShadowCry.Source.Project.MainInstance;
import TeamShadowCry.Source.Project.Process.Codec;
import TeamShadowCry.Source.Project.Process.ProcessInstance.ProcessInstance;

public class easy_tsccp implements Codec{

	@Override
	public String name() {
		// TODO Auto-generated method stub
		return "EASY TSCCP";
	}

	@Override
	public boolean UseLine() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public int toReadSize() {
		// TODO Auto-generated method stub
		return -1;
	}
	private String[] toArgs(String command){
		ArrayList<String> list = new ArrayList<String>();
		int startlength = 0;
		boolean isSelecting = false;
		String lastchar = "";
		for (int I=0;I<command.length();I++){
			String charget = command.substring(I,I+1);
			if (isSelecting){
				if (charget.equals("\"")){
					list.add(command.substring(startlength, I));
					isSelecting=false;
					startlength = I+1;
				}
			}else{
				if (charget.equals("\"")){
					startlength = I+1;
					isSelecting=true;
				}else{
					if (!lastchar.equals("")){
						if (charget.equals(" ") && !lastchar.equals(" ") && !lastchar.equals("\"")){
							list.add(command.substring(startlength, I));
							startlength = I+1;
						}
						if (charget.equals(" ") && lastchar.equals(" ")){
							startlength = I+1;
						}
					}
				}
			}
			lastchar = charget;
		}
		if (startlength!=command.length()-1){
			String get = command.substring(startlength, command.length());
			while (get.endsWith(" ")){
				get = get.substring(0, get.length()-1);
			}
			if (get.length()>0){
				list.add(get);
			}
		}
		return list.toArray(new String[list.size()]);
	}
	@Override
	public int aktion(byte[] val, byte[] Ram, byte[] extRam, byte[] all, InputStream fs, MainInstance given, ProcessInstance pr) throws IOException {
		return 0;
	}

	@Override
	public int aktion(byte[] val, byte[] Ram, byte[] extRam, byte[] all, BufferedReader fs,
		MainInstance given, ProcessInstance pr) throws IOException {
		int returning = 1;
		String[] cmd = toArgs(new String(val));
		for (String c : cmd){
			System.out.println(c);
		}
		return returning;
	}

}